
#include <iostream>

#include "bocat/common/tinyxml.hpp"

const char*
getValueByPath( TiXmlHandle root_handle, const char* path[] )
{
	TiXmlHandle	handle = root_handle;

	for ( int i=0; NULL != path[i]; ++i ) {
		handle = handle.FirstChildElement( path[i] );
	}

	TiXmlNode*	node_p = handle.Node();
	if ( NULL == node_p ) {
		return	NULL;
	}
	
	TiXmlNode*	text_np = node_p->FirstChild();
	if ( NULL == text_np ) {
		return	NULL;
	}

	return	text_np->Value();
}


void	do_some_parsing( const char* text )
{
	TiXmlDocument	doc;

	doc.Parse( text );

	/*
	TiXmlElement*	root_ep = doc.RootElement();
	if ( NULL == root_ep ) {
		std::cerr << "No root element found\n";
		return;
	}
	*/

	TiXmlNode*	document_np = doc.FirstChild( "document" );
	if ( NULL == document_np ) {
		std::cerr << "No <document> found\n";
		return;
	}
	TiXmlElement*	document_ep = document_np->ToElement();

	std::cout << "   status : " << document_ep->Attribute( "status" ) << std::endl
			  << "  product : " << document_ep->Attribute( "product" ) << std::endl
			  << " datatype : " << document_ep->Attribute( "datatype" ) << std::endl
			  << std::endl;

	TiXmlHandle	xml( &doc );
	TiXmlHandle	url_handle = xml.FirstChildElement("document").FirstChildElement("key").FirstChildElement("url");

	TiXmlNode*	url_np = url_handle.Node();
	if ( NULL == url_np ) {
		std::cerr << "No <url> found\n";
		return;
	}
	std::cout << " URL Node    : " << url_np->FirstChild()->Value() << std::endl;

	TiXmlElement*	url_ep = url_handle.Element();
	if ( NULL == url_ep ) {
		std::cerr << "No <url> found\n";
		return;
	}
	std::cout << " URL Element : " << url_ep->FirstChild()->Value() << std::endl;

	TiXmlHandle	document_handle = xml.FirstChildElement("document");

	// 'pkgid' : 'document/key/pkgid',
	TiXmlNode*	pkgid_np = document_handle.FirstChildElement("key").FirstChildElement("pkgid").Node();
	if ( NULL == pkgid_np ) {
		std::cerr << "No <document><key><pkgid> found\n";
	}
	else {
		const char*	pkgid = pkgid_np->FirstChild()->Value();
		if ( NULL == pkgid ) {
			std::cout << "  pkgid : NULL" << std::endl;
		}
		else {
			std::cout << "  pkgid : " << pkgid << std::endl;
		}
	}

	// 'cid' : 'document/wbng_storage_se/cid',
	const char* cid_path[] = {"wbng_storage_se","cid",NULL};
	const char*	cid = getValueByPath( document_handle, cid_path );
	if ( NULL == cid ) {
		std::cout << "  cid : NULL" << std::endl;
	}
	else {
		std::cout << "  cid : " << cid << std::endl;
	}

	// 'download_time' : 'document/vspider/metadata/downloadtime',
	const char* dltime_path[] = {"vspider","metadata","downloadtime",NULL};
	const char*	downloadtime = getValueByPath( document_handle, dltime_path );
	if ( NULL == downloadtime ) {
		std::cout << "  downloadtime : NULL" << std::endl;
	}
	else {
		std::cout << "  downloadtime : " << downloadtime << std::endl;
	}

	// 'title' : 'document/vspider/bbs-index/title',
	const char* title_path[] = {"vspider","bbs-index","title",NULL};
	const char*	title = getValueByPath( document_handle, title_path );
	if ( NULL == title ) {
		const char* title_path[] = {"vspider","blog-index","title",NULL};
		title = getValueByPath( document_handle, title_path );
	}
	if ( NULL == title ) {
		std::cout << "  title : NULL" << std::endl;
	}
	else {
		std::cout << "  title : " << title << std::endl;
	}

	// 'junk' : 'document/junk-filter/bbs_junk_type',
	const char* junk_path[] = {"junk-filter","bbs_junk_type",NULL};
	const char*	junk = getValueByPath( document_handle, junk_path );
	if ( NULL == junk ) {
		const char* junk_path[] = {"junk-filter","blog_junk_type",NULL};
		junk = getValueByPath( document_handle, junk_path );
	}
	if ( NULL == junk ) {
		std::cout << "  junk : NULL" << std::endl;
	}
	else {
		std::cout << "  junk : " << junk << std::endl;
	}

	// 'people_rank' : 'document/peoplerank/pr_rank',
	const char* peoplerank_path[] = {"peoplerank","pr_rank",NULL};
	const char*	peoplerank = getValueByPath( document_handle, peoplerank_path );
	if (NULL == peoplerank) {
		std::cout << "  peoplerank : NULL" << std::endl;
	}
	else {
		std::cout << "  peoplerank : " << peoplerank << std::endl;
	}

	// 'dup_title_id' : 'document/duplicate/title/repeat_id',
	const char* dup_title_id_path[] = {"duplicate","title","repeat_id", NULL};
	const char*	dup_title_id = getValueByPath( document_handle, dup_title_id_path );
	if ( NULL == dup_title_id ) {
		std::cout << "  dup_title_id : NULL" << std::endl;
	}
	else {
		std::cout << "  dup_title_id : " << dup_title_id << std::endl;
	}

	// 'dup_content_id' : 'document/duplicate/content/repeat_id',
	const char* dup_content_id_path[] = {"duplicate","content","repeat_id", NULL};
	const char*	dup_content_id = getValueByPath( document_handle, dup_content_id_path );
	if ( NULL == dup_content_id ) {
		std::cout << "  dup_content_id : NULL" << std::endl;
	}
	else {
		std::cout << "  dup_content_id : " << dup_content_id << std::endl;
	}


}


int	main ( void )
{
	{
		const char* text = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <document status=\"none\" product=\"bbs\" datatype=\"command\"><version>1.0</version><key><url><![CDATA[http://027club.com/bbs/thread-302-1-1.html]]></url> <md5><![CDATA[1102fb8d25f5900df24993d35329e0f2]]></md5></key><command><flags>8192</flags><f_dl_not_login>1</f_dl_not_login><f_login>0</f_login><f_junk>0</f_junk></command><junk-filter><errno>100</errno><bbs_junk_type><![CDATA[145]]></bbs_junk_type></junk-filter><duplicate><errno>100</errno><title><repeat_id/><repeat_type/></title><content><repeat_id/><repeat_type/></content></duplicate><peoplerank><errno>100</errno><pr_rank/></peoplerank><cluster><errno>0</errno><type>0</type><tag></tag><class></class></cluster></document>";

		do_some_parsing( text );
	}

	{
		const char* text = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <document status=\"add\" product=\"blog\" datatype=\"blog-page\"><version>1.0</version><key><url><![CDATA[http://007pure.blog.sohu.com/148924892.html ]]></url><md5>7c301b39180707762494d9e97ef64f35</md5></key><vspider source=\"BLOG-PAGE-SPIDER\"><errno>0</errno><metadata><url><![CDATA[http://007pure.blog.sohu.com/148924892.html ]]></url><baseurl><![CDATA[ ]]></baseurl><downloadtime>1271806965</downloadtime><content_type><![CDATA[text/xml;charset=utf-8]]></content_type><http_code>200</http_code></metadata><blog-index><title><![CDATA[青海4月26日拿出重建方案体现了组织有效反映迅速准确原则 ]]></title><titlelen>53</titlelen><author><![CDATA[深蓝 ]]></author><time>1271805170</time><replytime>0</replytime><modifytime>0</modifytime><viewcount>0</viewcount><replycount>0</replycount><flag>0</flag><add_time>0</add_time></blog-index><blog-seed><seedid>24263426</seedid><bspname><![CDATA[搜狐博客 ]]></bspname><sitename><![CDATA[深蓝 ]]></sitename><siteurl><![CDATA[http://007pure.blog.sohu.com/ ]]></siteurl><rssurl><![CDATA[http://007pure.blog.sohu.com/rss ]]></rssurl><bspurl><![CDATA[http://blog.sohu.com/ ]]></bspurl><host><![CDATA[007pure.blog.sohu.com ]]></host></blog-seed></vspider><docparser><version>1.0</version><errno>0</errno><openurl><![CDATA[http://007pure.blog.sohu.com/148924892.html  ]]></openurl><filesize>306</filesize><hostname>dc20.of.lfc.oohiq.net</hostname><index_data><author><![CDATA[深蓝  ]]></author><publictime>1271805170</publictime><replytime>0</replytime><modifytime>0</modifytime><title><![CDATA[青海4月26日拿出重建方案体现了组织有效反映迅速准确原则  ]]></title></index_data><floors><floornum>1</floornum><floor><floortext><![CDATA[深蓝咨询   胡占华=诗人古月=胡子曰：青海4月26日拿出重建方案体现了组织有效反映迅速准确原则[br]   玉树老百姓看到了党和政府领导的灾区人民和海外华人国外友人支持重建家园的信心与决心。[br]    深蓝咨询   胡占华=诗人古月=胡子[br] ]]></floortext><textsize>314</textsize><attach/></floor></floors></docparser><junk-filter><errno>0</errno><blog_junk_type>145</blog_junk_type></junk-filter><duplicate><errno>0</errno><title><repeat_id>322e967bcc4ea677c6fab1de5fa38d00</repeat_id><repeat_type>1</repeat_type></title><content><repeat_id>633ef0d52f1d2e706d9bee300fbd6100</repeat_id><repeat_type>1</repeat_type></content></duplicate><peoplerank><errno>0</errno><pr_rank>3121</pr_rank></peoplerank><cluster><errno>0</errno><type>0</type><tag></tag><class></class></cluster></document>";

		do_some_parsing( text );
	}

	{
		const char*	text = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <document status=\"add\" product=\"bbs\" datatype=\"bbs-page\"><version>1.0</version><key><url><![CDATA[http://zs54s1.chinaw3.com/viewthread.php?tid=31355 ]]></url><md5>d26a4d00c791820cfac1d2773cef1e27</md5></key><vspider source=\"BBS-SPIDER\"><errno>0</errno><metadata><url><![CDATA[http://zs54s1.chinaw3.com/viewthread.php?tid=31355 ]]></url><downloadtime>1271808749</downloadtime><downspeed>35738</downspeed><content_type><![CDATA[text/html]]></content_type><http_code>200</http_code></metadata><bbs-page><charset><![CDATA[GB18030]]></charset></bbs-page><bbs-index><title><![CDATA[旅游美食快递 ]]></title><titlelen>18</titlelen><author><![CDATA[昕凌 ]]></author><time>1271806975</time><replytime>1271806975</replytime><modifytime>1271806975</modifytime><viewcount>3</viewcount><replycount>0</replycount><flag>0</flag><add_time>0</add_time><fir_page_url_md5><![CDATA[99942802e551dec8868c23c5fc7f528b ]]></fir_page_url_md5><page_no>1</page_no></bbs-index><bbs-seed><seedid>93537905</seedid><seed_url><![CDATA[http://zs54s1.chinaw3.com/forumdisplay.php?fid=50 ]]></seed_url><forum><![CDATA[球迷之家 ]]></forum><chanl><![CDATA[黄蜂专区 ]]></chanl><host><![CDATA[zs54s1.chinaw3.com]]></host><site_id>5777319</site_id><rank>0</rank><tag><![CDATA[ ]]></tag></bbs-seed><bbs-stat><url><![CDATA[http://zs54s1.chinaw3.com/viewthread.php?tid=31355]]></url><url_md5><![CDATA[d26a4d00c791820cfac1d2773cef1e27]]></url_md5><extinfo>0</extinfo><flags>0</flags><f_dl_not_login>0</f_dl_not_login><f_login>0</f_login><command>1</command><cur_time>1271808749</cur_time></bbs-stat></vspider><docparser><version>3.0.0</version><errno>0</errno><openurl><![CDATA[http://zs54s1.chinaw3.com/viewthread.php?tid=31355 ]]></openurl><hostname>dc19.of.lfc.oohiq.net</hostname><next_pages><page><url><![CDATA[ ]]></url><no>-1</no></page></next_pages><index_data><author><![CDATA[昕凌 ]]></author><publictime>1271805060</publictime><replytime>1271806975</replytime><modifytime>1271806975</modifytime><title><![CDATA[旅游美食快递 ]]></title></index_data><floors><floor><floortext><![CDATA[[img]http://qiumizhijia.com.cn/center/avatar.php?uid=1415&size=middle [/img] ]]></floortext><textsize>0</textsize><attach><image><f_url><![CDATA[http://qiumizhijia.com.cn/center/avatar.php?uid=1415&size=middle ]]></f_url><md5>53fe074a690134b0f7b09072bb0eb317</md5><w>0</w><h>0</h><desc/><score>0</score></image></attach></floor></floors></docparser><junk-filter><errno>0</errno><bbs_junk_type>32</bbs_junk_type></junk-filter><duplicate><errno>0</errno><title><repeat_id>1c2132c70e307e21c473b8941bbd9800</repeat_id><repeat_type>1</repeat_type></title><content><repeat_id>1c2132c70e307e21c473b8941bbd9800</repeat_id><repeat_type>1</repeat_type></content></duplicate><peoplerank><errno>0</errno><pr_rank>279</pr_rank></peoplerank><cluster><errno>0</errno><type>0</type><tag></tag><class></class></cluster></document>";
		do_some_parsing( text );
	}

	return	0;
}

